// -----------------------------------------------------------------------
//  <copyright file="OpenApiModule.cs" company="LiuliuSoft">
//      Copyright (c) 2025 66SOFT. All rights reserved.
//  </copyright>
//  <site>https://ifs.66soft.net</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-02-04 15:02</last-date>
// -----------------------------------------------------------------------

using DaprPlus.AspNetCore;
using DaprPlus.Builder;

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;


namespace Shares.Infrastructure.AspNetCore;

public class OpenApiModule : AspModuleBase
{
    /// <summary>获取 模块级别，级别越小越先启动</summary>
    public override ModuleLevel Level => ModuleLevel.Application;

    /// <summary>
    /// 获取 模块启动顺序，模块启动的顺序按级别启动，同一级别内部再按此顺序启动
    /// 级别默认为0，表示无依赖，需要在同级别有依赖顺序的时候，再重写为&gt;0的顺序值
    /// </summary>
    public override int Order => 998;

    /// <summary>将模块服务添加到依赖注入服务容器中</summary>
    /// <param name="services">依赖注入服务容器</param>
    /// <returns></returns>
    public override IServiceCollection AddServices(IServiceCollection services)
    {
        // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
        services.AddOpenApi();
        return base.AddServices(services);
    }

    /// <summary>应用AspNetCore的服务业务</summary>
    /// <param name="app">Web应用程序</param>
    public override Task UseModule(WebApplication app)
    {
        if (app.Environment.IsDevelopment())
        {
            app.MapOpenApi();
        }

        return base.UseModule(app);
    }
}
